<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('app/libraries/excel/PHPExcel.php');
$chart = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
$page_title = "Thống kê tài chính từ ngày ". date('d/m/Y', strtotime($startdate))." đến ngày ". date('d/m/Y', strtotime($enddate));
		// Create new PHPExcel object
    	$objPHPExcel = new PHPExcel();
    	// Set properties
    	$objPHPExcel->getProperties()->setCreator("AdMarket")
    								 ->setLastModifiedBy("AdMarket")
    								 ->setTitle("Office 2007 XLSX Document")
    								 ->setSubject("Office 2007 XLSX Document")
    								 ->setDescription($page_title)
    								 ->setCategory("");
	
	   $styleArray = array(
			'font' => array(
				'bold' => true,
			),
			'alignment' => array(
				'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			),
			'borders' => array(
				'top' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
				),
			)
		);
	    $objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
    	$objPHPExcel->getActiveSheet()->mergeCells('A2:C2');
    	$objPHPExcel->getActiveSheet()->mergeCells('D2:H2');
    	$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $page_title);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A2', 'Theo: '.$t_time);
		$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D2', 'Đại lý: '.$get_group_name);
    	$objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray($styleArray);
    	$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getStyle('E2')->applyFromArray($styleArray);
		
    	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
    	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
    	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
		$objPHPExcel->setActiveSheetIndex(0)
    				->setCellValue('A3', '#')
    	            ->setCellValue('B3', 'Tên')
    	            ->setCellValue('C3', 'Ngày')
    	            ->setCellValue('D3', 'Tiền đã nạp (VND)')
    	            ->setCellValue('E3', 'Tiền đã sử dụng (VND)')
    	            ->setCellValue('F3', 'Tiền khuyến mại (VND)')
    				;
		
		$index = 4;
        foreach($items as $item)
        { 
        	$objPHPExcel->setActiveSheetIndex(0)
    					->setCellValue('A'.$index, $index-3)
            			->setCellValue('B'.$index, $item->username)
            			->setCellValue('C'.$index, $item->time)
            			->setCellValue('D'.$index, number_format($item->ad))
            			->setCellValue('E'.$index, number_format($item->sub))
    					->setCellValue('F'.$index, number_format($item->promotion));
    		$index++;
        }
        $objPHPExcel->getActiveSheet()->mergeCells('A'.$index.':C'.$index);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$index, 'Tổng');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$index, number_format($summary->ad));
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$index, number_format($summary->sub));
		$objPHPExcel->setActiveSheetIndex(0)->setCellValue('F'.$index, number_format($summary->promotion));
        
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="AdMarket_transaction_report.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');   
    	$objWriter->save('php://output');
    	exit;